
#include <boost/test/unit_test.hpp>

#include "HeaderFileAnalyzer.h"
#include "Util.h"

using namespace analyzer;


BOOST_AUTO_TEST_SUITE(Analyzer)

BOOST_AUTO_TEST_CASE(acceptAnalyzer_non_existant)
{
	std::ostringstream	out;
	setLogLevel(INFO);
	gpOut = &out;
	
	bool recursive = false;
	std::string path;
	
	HeaderFileAnalyzer analyzer;
	analyzer.setIncludeSubDirectories(true);
	
	BOOST_REQUIRE_THROW(analyzer.scanDirectory(std::string("test_files/non_existant")), typename boost::filesystem3::filesystem_error);
	
	gpOut = &std::cout;
}

BOOST_AUTO_TEST_CASE(acceptAnalyzer_test)
{
	std::ostringstream	out;
	setLogLevel(INFO);
	gpOut = &out;
	
	bool recursive = false;
	std::string path;
	
	struct test : public HeaderFileAnalyzer {
		void test_cycles() {
			std::vector<std::vector<int> > cyclicVertices = cycleFinderAlgorithm->findCycle(*dependencyGraph);
			BOOST_REQUIRE(cyclicVertices.size() == 0);
		}
	} analyzer;
	analyzer.setIncludeSubDirectories(true);
	
	BOOST_CHECK_NO_THROW(analyzer.scanDirectory(std::string("test_files/test")));
	
	analyzer.test_cycles();
	
	gpOut = &std::cout;
}

BOOST_AUTO_TEST_CASE(acceptAnalyzer_testmultiple_nonrecursive)
{
	std::ostringstream	out;
	setLogLevel(INFO);
	gpOut = &out;
	
	bool recursive = false;
	std::string path;
	
	struct test : public HeaderFileAnalyzer {
		void test_cycles() {
			std::vector<std::vector<int> > cyclicVertices = cycleFinderAlgorithm->findCycle(*dependencyGraph);
			BOOST_REQUIRE(cyclicVertices.size() == 0);
		}
	} analyzer;
	analyzer.setIncludeSubDirectories(false);
	
	BOOST_CHECK_NO_THROW(analyzer.scanDirectory(std::string("test_files/test-multiple")));
	
	analyzer.test_cycles();
	
	gpOut = &std::cout;
}

BOOST_AUTO_TEST_CASE(acceptAnalyzer_testmultiple_recursive)
{
	std::ostringstream	out;
	setLogLevel(INFO);
	gpOut = &out;
	
	bool recursive = false;
	std::string path;
	
	struct test : public HeaderFileAnalyzer {
		void test_cycles() {
			std::vector<std::vector<int> > cyclicVertices = cycleFinderAlgorithm->findCycle(*dependencyGraph);
			BOOST_REQUIRE(cyclicVertices.size() == 2);
			
			boost::property_map<Graph, boost::vertex_name_t>::type vertexNames = boost::get(boost::vertex_name_t(), *dependencyGraph);
			
			std::vector<std::vector<int> >::iterator iter(cyclicVertices.begin());
			
			BOOST_REQUIRE(iter->size() == 5);
			std::vector<int>::iterator it(iter->begin());
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "a.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "g.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "c.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "b.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "a.h");
			
			++iter;
			BOOST_REQUIRE(iter->size() == 5);
			it = iter->begin();
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "c.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "f.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "e.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "d.h");
			BOOST_REQUIRE(boost::get(vertexNames, *it++) == "c.h");
		}
	} analyzer;
	analyzer.setIncludeSubDirectories(true);
	
	BOOST_CHECK_NO_THROW(analyzer.scanDirectory(std::string("test_files/test-multiple")));
	
	analyzer.test_cycles();
	
	gpOut = &std::cout;
}

BOOST_AUTO_TEST_SUITE_END()

